//
//  ZDStatusTopView.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/11/2.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDStatusTopView.h"
#import <Masonry/Masonry.h>
#import "UIImageView+Extension.h"
#import "ZDStatus.h"
#import "UILabel+Extension.h"
#import "NSDate+Extension.h"

@interface ZDStatusTopView ()

/**
 *  分割视图
 */
@property (nonatomic, strong) UIView *separatView;

/**
 *  用户头像
 */
@property (nonatomic, strong) UIImageView *iconView;

/**
 *  用户姓名
 */
@property (nonatomic, strong) UILabel *nameLabel;

/**
 *  微博创建时间
 */
@property (nonatomic, strong) UILabel *timeLabel;

/**
 *  微博来源
 */
@property (nonatomic, strong) UILabel *sourceLabel;

/**
 *  vip标识
 */
@property (nonatomic, strong) UIImageView *vipView;

/**
 *  认证标识
 */
@property (nonatomic, strong) UIImageView *verifiedView;

@end

@implementation ZDStatusTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self prepareUI];
    }
    
    return self;
}


- (void)prepareUI
{
//    self.backgroundColor = [UIColor brownColor];
    
    //添加子控件
    [self addSubview:self.separatView];
    [self addSubview:self.iconView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.vipView];
    [self addSubview:self.verifiedView];
    
    
    //添加约束
    [_separatView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.mas_equalTo(self);
        make.height.mas_equalTo(10);
    }];
    
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self).offset(margin);
        make.top.mas_equalTo(_separatView.mas_bottom).offset(margin);
        make.width.height.mas_equalTo(35);
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_iconView.mas_right).offset(margin);
        make.top.mas_equalTo(_iconView);
    }];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_iconView.mas_right).offset(margin);
        make.bottom.mas_equalTo(_iconView.mas_bottom);
    }];
    
    [_sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_timeLabel.mas_right).offset(margin);
        make.bottom.mas_equalTo(_timeLabel);
    }];
    
    [_vipView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_nameLabel.mas_right).offset(margin);
        make.top.mas_equalTo(_nameLabel);
        make.width.height.mas_equalTo(14);
    }];
    
    [_verifiedView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(_iconView.mas_right).offset(8.5);
        make.bottom.mas_equalTo(_iconView.mas_bottom).offset(8.5);
        make.width.height.mas_equalTo(17);
    }];
}


- (void)setStatus:(ZDStatus *)status
{
    _status = status;
    
    [self.iconView zd_setImageWithURL:[NSURL URLWithString:status.user.profile_image_url]];
    
    self.nameLabel.text = status.user.name;
    
    self.timeLabel.text = [NSDate weiboDateStringWithDate:[NSDate dateWithSinaDateString:status.created_at]];
    
    self.sourceLabel.text = status.source;
    
    self.vipView.image = status.user.mbrankImage;
    
    self.verifiedView.image = status.user.verifiedImage;
}


#pragma mark 懒加载
- (UIView *)separatView
{
    if (!_separatView)
    {
        _separatView = [[UIView alloc] init];
        
        _separatView.backgroundColor = [UIColor colorWithWhite:0.93 alpha:1.0];
    }
    
    return _separatView;
}

- (UIImageView *)iconView
{
    if (!_iconView)
    {
        _iconView = [[UIImageView alloc] init];
    }
    
    return _iconView;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel)
    {
        _nameLabel = [[UILabel alloc] initWithFontSize:14 color:[UIColor darkGrayColor]];
    }
    
    return _nameLabel;
}

- (UILabel *)timeLabel
{
    if (!_timeLabel)
    {
        _timeLabel = [[UILabel alloc] initWithFontSize:12 color:[UIColor orangeColor]];
    }
    
    return _timeLabel;
}

- (UILabel *)sourceLabel
{
    if (!_sourceLabel)
    {
        _sourceLabel = [[UILabel alloc] initWithFontSize:12 color:[UIColor lightGrayColor]];
    }
    
    return _sourceLabel;
}

- (UIImageView *)vipView
{
    if (!_vipView)
    {
        _vipView = [[UIImageView alloc] init];
    }
    
    return _vipView;
}

- (UIImageView *)verifiedView
{
    if (!_verifiedView)
    {
        _verifiedView = [[UIImageView alloc] init];
    }
    
    return _verifiedView;
}

@end
